package pathutil

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"
)

const env_windir = "windir"

func GetExplorerPath() string {
	return fmt.Sprintf(`%s\explorer.exe`, os.Getenv(env_windir))
}

func GetCmdPath() string {
	return fmt.Sprintf(`%s\system32\cmd.exe`, os.Getenv(env_windir))
}

func GetRundll32Path() string {
	return fmt.Sprintf(`%s\system32\rundll32.exe`, os.Getenv(env_windir))
}

func GetPhotoViewerDllPath() string {
	return fmt.Sprintf(`%s\Windows Photo Viewer\PhotoViewer.dll`, os.Getenv("ProgramFiles"))
}

func IsBatFile(path string) bool {
	flag := false
	ext := strings.TrimSpace(strings.ToLower(filepath.Ext(path)))
	if ".bat" == ext || ".cmd" == ext {
		flag = true
	}
	return flag
}

// WrapQuotOnPathItems 包裹路径中的空格
// d:\ab\x y z\hoho.bat -> d:\ab\"x y z"\hoho.bat
// 不是整个包裹起来 "d:\ab\x y z\hoho.bat"
func WrapQuotOnPathItems(path string) string {
	sep := string([]rune{filepath.Separator})
	items := strings.Split(filepath.Clean(path), sep)
	handledPath := ""
	for ind, item := range items {
		target := item
		if strings.Contains(item, " ") {
			target = `"` + target + `"`
		}
		if ind > 0 {
			handledPath += sep
		}
		handledPath += target
	}
	return handledPath
}
